package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.MyException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public R saveWithFlavor(DishDto dishDto) {
        save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        Long dishId = dishDto.getId();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            if (flavor.getValue().equals("[]")) {
                throw new MyException("请检查口味是否正确");
            }
        }
//        flavors = flavors.stream().map((item)->{
//            item.setDishId(dishId);
//            return item;
//        }).collect(Collectors.toList());

//        flavors.forEach((flavor)->{
//            flavor.setDishId(dishId);
//        });

        dishFlavorService.saveBatch(flavors);

        return R.success(flavors);
    }

    @Override
    public R getByIdDish(Long id) {
        DishDto dishDto = new DishDto();
        Dish dish = super.getById(id);
        BeanUtils.copyProperties(dish, dishDto);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(lqw);
        dishDto.setFlavors(dishFlavors);
        return R.success(dishDto);
    }

    @Override
    @Transactional
    public R updateDish(DishDto dishDto) {
        updateById(dishDto);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        boolean b = dishFlavorService.saveBatch(flavors);
        if (b) {
            return R.success("修改成功");
        }

        return R.error("修改失败");
    }

    @Override
    public R updateDishStatus(int id, Long[] ids) {
        List<Dish> list = new ArrayList<>();
        for (Long aLong : ids) {
            Dish dish = new Dish();
            dish.setStatus(id);
            dish.setId(aLong);
            list.add(dish);
        }

        boolean b = this.updateBatchById(list);
        if (b) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @Override
    @Transactional
    public R deleteDish(List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId, ids).eq(Dish::getStatus, 1);
        int count = count(lqw);
        if (count > 0) {
            throw new MyException("套餐正在售卖中，删除失败");
        }
        removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lqw2);
        return R.success("删除成功");
    }
}
